/*
 * ClusterTool.java
 *
 * Version:
 *      $Id$
 *
 * Copyright (c) 2009 Peter O. Erickson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without major
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, and/or distribute copies of, but
 * under no circumstances sublicense and/or sell, the Software,
 * and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package edu.rit.poe.cluster;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *
 * @author  Peter O. Erickson
 *
 * @version $Id$
 */
public class ClusterTool {
    
    /** The default file for the list of servers. */
    public static final String DEFAULT_SERVERS_FILE = "servers.conf";
    
    /** The runtime instance from which to launch processes. */
    private static final Runtime RUNTIME = Runtime.getRuntime();
    
    /**
     * Runs the clustertool.
     * 
     * @param   args    command line arguments (see documentation)
     */
    public static void main( String... args ) {
        
        if ( args[ 0 ].equals( "start" ) ) {
            try {
                start( null );
            } catch ( IOException e ) {
                //TODO add verbose error output
                e.printStackTrace();
            } catch ( Exception e ) {
                //TODO add verbose error output
                e.printStackTrace();
            }
        } else if ( args[ 0 ].equals( "stop" ) ) {
            try {
                stop( null );
            } catch ( IOException e ) {
                //TODO add verbose error output
                e.printStackTrace();
            } catch ( Exception e ) {
                //TODO add verbose error output
                e.printStackTrace();
            }
        }
        
        
    }
    
    public static void start( String file ) throws Exception {
        if ( file == null ) {
            file = DEFAULT_SERVERS_FILE;
        }
        BufferedReader in = new BufferedReader( new FileReader( file ) );
        
        String server = null;
        while ( ( server = in.readLine() ) != null ) {
            Process process = RUNTIME.exec( "ssh " + server + " java -classpath"
                    + " jcluster-v1.0rc3-lib.jar"
                    + " edu.rit.poe.cluster.BackendNode &" );
            
        }
    }
    
    public static void stop( String file ) throws Exception {
        if ( file == null ) {
            file = DEFAULT_SERVERS_FILE;
        }
        BufferedReader in = new BufferedReader( new FileReader( file ) );
        
        String server = null;
        while ( ( server = in.readLine() ) != null ) {
            
            Socket socket = new Socket( server, 4244 );
            BufferedReader socketin = new BufferedReader(
                    new InputStreamReader( socket.getInputStream() ) );
            try {
                socketin.readLine(); // <-- this will fail, move on to the next
            } catch ( Exception e ) {
                continue;
            }
        }
    }
    
} // ClusterTool
